package org.cscarioni

class ChartValuesCalculatorService {

    boolean transactional = true

    def calculateChartMap(estimatedAwareCollection,totalDatesRange,initialEstimate) {
    	def chartMap = new LinkedHashMap();
		def currentEstimateValue = initialEstimate
		def lastDayElementInCollection=getLastDayFromEstimatedCollection(estimatedAwareCollection)
		for(itdate in totalDatesRange){
			def dateString = itdate.format("dd/MM")
			if(lastDayElementInCollection==dateString){
				break
			}
			def estimatedAware = getEstimatedAwareForDate(dateString,estimatedAwareCollection)
			if(estimatedAware){
				currentEstimateValue=estimatedAware.estimatedTime
				chartMap."${dateString}"=estimatedAware.estimatedTime
			}
			else{
				chartMap."${dateString}"=currentEstimateValue
			}
		}
		
		return chartMap
    }
	
	def getLastDayFromEstimatedCollection(estimatedAwareCollection){
		def now=new Date()
		return (now+1).format("dd/MM")
	}
	
	def getEstimatedAwareForDate(dateString,estimatedAwareCollection){
		for(estimatedAware in estimatedAwareCollection){
			if(estimatedAware.day.format('dd/MM')==dateString){
				return estimatedAware
			}			
		}
		return null
	}
	def calculateToZeroProgressionForDays(sprint){
		def estimatedOriginal = sprint.estimatedTimes.iterator().next()
		def days = sprint.endDate-sprint.startDate
		return calculateToZeroProgressionForDays(estimatedOriginal,days)
	}
	
	def calculateToZeroProgressionForDays(estimatedOriginal,days){
		def diferencial = estimatedOriginal/days
		def eachValueHolder=estimatedOriginal
		def list = []
		(1..days).each{
			list<<eachValueHolder
			eachValueHolder -= diferencial			
		}
		return list
	}
}
